/**
 * 
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import edu.pku.notation.runtime.draw2d.RoundedRectangleFigure;
import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @author YanYan
 *
 */
public class ArtifactFigure extends CommonFigure {
	public IFigure root;

	public IFigure body;
	
	public IFigure getBody() {
		return body;
	}
	
	private TextFigure artifactLable = null;
	
	public ArtifactFigure(final Label header){
		
		super();

		XYLayout layout = new XYLayout();
		setLayoutManager(layout);
		setForegroundColor(ColorConstants.blue);
		setBorder(new LineBorder(1));
		
		root = new RoundedRectangleFigure();
		ToolbarLayout rootLayout = new ToolbarLayout();
		rootLayout.setVertical(true);
		root.setLayoutManager(rootLayout);
		root.setForegroundColor(ColorConstants.black);
		
		if (header.getText().length() > 0) {
			root.add(header);
		}
		
		artifactLable = new TextFigure();
		artifactLable.setOpaque(false);
		artifactLable.setFont(ResourceFactory.getHeadFont());
		root.add(artifactLable);
		
		body = new RoundedRectangleFigure();
		XYLayout xyL = new XYLayout();
		body.setLayoutManager(xyL);
		body.setOpaque(true);
		root.add(body);
		
		add(root);
				
	}
	
	public void paint(Graphics graphics){
		
		super.paint(graphics);
		
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		Rectangle figureArea = getBounds();
		
		graphics.setForegroundColor(ColorConstants.buttonDarkest);
		
		int width = figureArea.width;		
		int relativeX = figureArea.x+width;
		int relativeY = figureArea.y;
		
		Point upLeft = new Point(relativeX-25, relativeY+7);
		Point downLeft = new Point(relativeX-25, relativeY+32);
		Point downRight = new Point(relativeX-7, relativeY+32);
		
		Point RU_upleft = new Point(relativeX-12, relativeY+7);
		Point RU_downleft = new Point(relativeX-12, relativeY+12);
		Point RU_downright = new Point(relativeX-7, relativeY+12);
		
		graphics.drawLine(downLeft, downRight);
		graphics.drawLine(downLeft, upLeft);
		graphics.drawLine(upLeft, RU_upleft);
		graphics.drawLine(RU_upleft, RU_downright);
		graphics.drawLine(RU_downright, downRight);
		graphics.drawLine(RU_upleft, RU_downleft);
		graphics.drawLine(RU_downleft, RU_downright);
		
		graphics.popState();
	}
	
	public TextFigure getArtifactLable() {
		return artifactLable;
	}
	
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		if (root != null)
			root.setSize(rect.width, rect.height);
		if (body != null)
			body.setSize(rect.width, rect.height);
	}

}
